package com.exam.online.system.service.impl;

import com.exam.online.system.dao.*;
import com.exam.online.system.entity.*;
import com.exam.online.system.service.UserService;
import com.exam.online.system.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author zp
 * @program online-exam-system
 * @description
 * @create 2019-05-05 19:49
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private TeacherDAO teacherDAO;
    @Autowired
    private StudentDAO studentDAO;
    @Autowired
    private RoleDAO roleDAO;
    @Autowired
    private CourseDAO courseDAO;
    @Autowired
    private TeacherCourseDAO teacherCourseDAO;
    @Autowired
    private StuCourseRelationDAO stuCourseRelationDAO;


    @Override
    public Teacher teacherLogin(String account, String password) {

        return teacherDAO.teacherLogin(account, password);

    }

    @Override
    public Student stuLoggin(String account, String password) {

        return studentDAO.stuLoggin(account, password);

    }

    @Override
    public List<Role> findRoles() {
        return roleDAO.findRoles();
    }

    @Transactional
    @Override
    public Integer registerStudent(String account, String password,
                                   Integer sex, Integer grade, String name, Integer stuNum,
                                   String courseList) {
        Student student = new Student();
        student.setAcount(account);
        student.setPassword(password);
        student.setCreateTime(new Date());
        student.setName(name);
        student.setGrade(grade);
        student.setSex(sex);
        student.setState(1);
        student.setStuNum(stuNum);
        studentDAO.insert(student);
        Integer id = student.getId();
        String[] split = courseList.split(";");
        for (String sp : split) {
            StuCourseRelation stuCourseRelation = new StuCourseRelation();
            stuCourseRelation.setCourseId(Integer.parseInt(sp));
            stuCourseRelation.setCreateTime(new Date());
            stuCourseRelation.setStuId(id);
            stuCourseRelation.setState(1);
            stuCourseRelationDAO.insert(stuCourseRelation);
        }
        return 1;

    }

    @Override
    public PageModel getAllCourse(Integer pageNo, Integer pageSize) {
        PageModel<Course> pageModel = new PageModel<>();
        pageModel.setPageNo(pageNo);
        pageModel.setPageSize(pageSize);
        List<Course> paperList = courseDAO.getAllCourse((pageNo - 1) * pageSize, pageSize);
        Integer total = courseDAO.getAllCourseCount();
        pageModel.setDatas(paperList);
        pageModel.setTotalPages(total / pageSize + 1);
        pageModel.setTotalRecords(total);
        return pageModel;
    }

    @Transactional
    @Override
    public Integer registerTeacher(String account, String password, Integer courseId, String name, Integer sex) {


        Teacher teacher = new Teacher();
        teacher.setAccount(account);
        teacher.setCreateTime(new Date());
        teacher.setName(name);
        teacher.setPassword(password);
        teacher.setSex(sex);
        teacher.setState(1);
        teacherDAO.insert(teacher);

        //老师和课程关系添加
        TeacherCourse teacherCourse = new TeacherCourse();
        teacherCourse.setCourseId(courseId);
        teacherCourse.setCreateTime(new Date());
        teacherCourse.setState(1);
        teacherCourse.setTeacherId(teacher.getId());
        int insert1 = teacherCourseDAO.insert(teacherCourse);


        return insert1;
    }

    @Override
    public PageModel getAllTeacher(Integer pageNo, Integer pageSize) {
        PageModel<Teacher> pageModel = new PageModel<>();
        pageModel.setPageNo(pageNo);
        pageModel.setPageSize(pageSize);
        List<Teacher> paperList = teacherDAO.getAllTeacher((pageNo - 1) * pageSize, pageSize);
        Integer total = teacherDAO.getAllTeacherCount();
        pageModel.setDatas(paperList);
        pageModel.setTotalPages(total / pageSize + 1);
        pageModel.setTotalRecords(total);
        return pageModel;
    }

    @Override
    public PageModel getAllStudent(Integer pageNo, Integer pageSize) {
        PageModel<Student> pageModel = new PageModel<>();
        pageModel.setPageNo(pageNo);
        pageModel.setPageSize(pageSize);
        List<Student> paperList = studentDAO.getAllStudent((pageNo - 1) * pageSize, pageSize);
        Integer total = studentDAO.getAllStudentCout();
        pageModel.setDatas(paperList);
        pageModel.setTotalPages(total / pageSize + 1);
        pageModel.setTotalRecords(total);
        return pageModel;
    }

    @Override
    public Integer editTeacher(Integer teacherId, String account, String password, String name) {
        Teacher teacher = new Teacher();
        teacher.setId(teacherId);
        teacher.setAccount(account);
        teacher.setPassword(password);
        teacher.setCreateTime(new Date());
        teacher.setName(name);
        return teacherDAO.updateByPrimaryKeySelective(teacher);

    }

    @Override
    public Teacher getTeacherById(Integer teacherId) {
        return teacherDAO.selectByPrimaryKey(teacherId);
    }

    @Override
    public Integer deleteTeacher(Integer teacherId) {

        return teacherDAO.deleteTeacher(teacherId);
    }

    @Override
    public Integer deleteStu(Integer stuId) {
        Student student = new Student();
        student.setId(stuId);
        student.setState(2);
        return studentDAO.updateByPrimaryKeySelective(student);

    }

    @Override
    public Integer editStu(Integer stuId, String account, String password, String name, Integer grade) {
        Student student = new Student();
        student.setId(stuId);
        student.setState(1);
        student.setAcount(account);
        student.setPassword(password);
        student.setName(name);
        student.setGrade(grade);
        return studentDAO.updateByPrimaryKeySelective(student);


    }

    @Override
    public Student getStuById(Integer stuId) {
        return studentDAO.selectByPrimaryKey(stuId);
    }

    @Override
    public boolean selectBystuNum(Integer stuNum) {
        Student student = studentDAO.selectByStuNum(stuNum);
        if (student == null) {
            return false;
        }
        return true;
    }


}
